//
//  PerWalletNewViewController.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/9/4.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "PerWalletNewViewController.h"
#import "PerWalletTableViewCell.h"
#import "WalletHeaderCell.h"
#import "PerWalletRecordViewController.h"
#import "CCZSegementController.h"
#import "PerWalletViewController.h"
#import "DepositChargeViewController.h"
#import "BRDatePickerView.h"
#import "WKWebViewController.h"
#import "DepositRecordViewController.h"
#import "IncomeDetailViewController.h"
#import <AXWebViewController.h>

#import "AliPayServer.h"
#import <AlipaySDK/AlipaySDK.h>

@interface PerWalletNewViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UILabel *depositText;
@property (weak, nonatomic) IBOutlet UILabel *deposit;
@property (weak, nonatomic) IBOutlet UIButton *depositCharge;
@property (strong, nonatomic) UILabel *balanceText;
@property (strong, nonatomic) UILabel *balance;
@property (strong, nonatomic) UILabel *frozenBalance;
@property (strong, nonatomic) NSString *withdrawableMoney;
@property (strong, nonatomic) UIButton *balanceWithdraw;

@property (weak, nonatomic) IBOutlet UIView *virtualCoinView;
@property (weak, nonatomic) IBOutlet UIView *chooseDateView;
@property (weak, nonatomic) IBOutlet UIButton *virtualCoinCharge;
@property (weak, nonatomic) IBOutlet UILabel *virtualCoin;
@property (weak, nonatomic) IBOutlet UILabel *startDate;
@property (weak, nonatomic) IBOutlet UILabel *endDate;
@property (weak, nonatomic) IBOutlet UIButton *startDateBtn;
@property (weak, nonatomic) IBOutlet UIButton *endDateBtn;
@property (weak, nonatomic) IBOutlet UILabel *receivedVirtualCoin;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topViewH;

@end

@implementation PerWalletNewViewController

- (void)viewWillAppear:(BOOL)animated
{
	[self updateUserInfo];
}

- (void)viewDidLoad {
    [super viewDidLoad];
	[self createUI];
	[self updateUserInfo];
    // Do any additional setup after loading the view from its nib.
}

- (void)updateViewConstraints
{
	[super updateViewConstraints];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createUI {
	self.navigationItem.title = @"我的钱包";
	//self.scrollView.backgroundColor = UISeparatedColorRGB;
	self.contentView.backgroundColor = UISeparatedColorRGB;
	self.automaticallyAdjustsScrollViewInsets = NO;
	self.scrollView.scrollEnabled = YES;
	UIButton * right = [UIButton buttonWithType:UIButtonTypeCustom];
	[right setTitle:@"支出记录" forState:UIControlStateNormal];
	right.titleLabel.font = kUHSystemFontWithSize(14);
	right.frame = CGRectMake(0, 0, [LZCTools textSizeWithText:right.titleLabel.text font:right.titleLabel.font].width, 44);
	right.titleLabel.textAlignment = NSTextAlignmentRight;
	[right setTitleColor:kWhiteColor forState:UIControlStateNormal];
	[right addTarget:self action:@selector(recordMoney:) forControlEvents:UIControlEventTouchUpInside];
	self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:right];
	
	self.topView.backgroundColor = UIMainColorRGB;
	self.depositCharge.layer.borderColor = [kWhiteColor CGColor];
	self.depositCharge.layer.borderWidth = 1;
	self.depositCharge.layer.cornerRadius = 15;
	NSInteger roleNum = [GetUserDefaule(USERINFO)[@"role"] integerValue];
	if (roleNum == 3 || roleNum == 4 || roleNum == 6) {
		self.topView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SH(3.5));
		self.topViewH.constant = SH(3.5);
		_balanceText = [[UILabel alloc] initWithFrame:CGRectMake(self.depositText.x, SH(6)+10, SW(3), self.depositText.height)];
		_balanceText.textColor = kWhiteColor;
		_balanceText.font = [UIFont systemFontOfSize:17];
		_balanceText.text = @"账户余额(元)";
		[self.topView addSubview:_balanceText];
		_balance = [[UILabel alloc] initWithFrame:CGRectMake(_balanceText.x, _balanceText.y+_balanceText.height+10, SW(3), self.deposit.height)];
		_balance.text = @"0";
		_balance.textColor = kWhiteColor;
		_balance.font = [UIFont systemFontOfSize:20];
		[self.topView addSubview:_balance];
		
		_frozenBalance = [[UILabel alloc] initWithFrame:CGRectMake(_balance.x+_balance.width, _balance.y+5, SW(4), 10)];
		_frozenBalance.text = @"120元冻结中";
		_frozenBalance.textColor = kWhiteColor;
		_frozenBalance.font = [UIFont systemFontOfSize:9];
		[self.topView addSubview:_frozenBalance];
		
		_balanceWithdraw = [[UIButton alloc] initWithFrame:CGRectMake(self.depositCharge.x, _balanceText.y+_balanceText.height, self.depositCharge.width, self.depositCharge.height)];
		[_balanceWithdraw setTitle:@"提现" forState:UIControlStateNormal];
		[_balanceWithdraw addTarget:self action:@selector(withdrawalClick:) forControlEvents:UIControlEventTouchUpInside];
		[_balanceWithdraw setTitleColor:kWhiteColor forState:UIControlStateNormal];
		[_balanceWithdraw.titleLabel setFont:[UIFont systemFontOfSize:15]];
		_balanceWithdraw.layer.borderColor = [kWhiteColor CGColor];
		_balanceWithdraw.layer.borderWidth = 1;
		_balanceWithdraw.layer.cornerRadius = 15;
		[self.topView addSubview:_balanceWithdraw];
	}
	[self.virtualCoinCharge setTitleColor:UIMainColorRGB forState:UIControlStateNormal];
	self.virtualCoinView.frame = CGRectMake(0, self.topView.y+self.topView.height, SCREEN_WIDTH, 150);
	self.virtualCoinCharge.layer.borderColor = [UIMainColorRGB CGColor];
	self.virtualCoinCharge.layer.borderWidth = 1;
	self.virtualCoinCharge.layer.cornerRadius = 15;
	self.chooseDateView.frame = CGRectMake(0, self.virtualCoinView.y+self.virtualCoinView.height+1, SCREEN_WIDTH, 35);
	self.scrollH.constant = SCREEN_HEIGHT+self.chooseDateView.y-64;
	NSLog(@"scroll y is %lf", self.scrollH.constant);
	NSArray *titleArr;
	DepositRecordViewController *depositRecord = [[DepositRecordViewController alloc] init];
	IncomeDetailViewController *incomeDetail = [[IncomeDetailViewController alloc] init];
	NSArray *vcArr;
	if (roleNum == 3 || roleNum == 4 || roleNum == 6) {
		titleArr = @[@"收入明细",@"面试服务费"];
		vcArr = @[incomeDetail,depositRecord];
	}
	else{
		titleArr = @[@"面试服务费"];
		 vcArr = @[depositRecord];
	}
	CCZSegementController *segment = [[CCZSegementController alloc] initWithFrame:CGRectMake(0, self.chooseDateView.y+self.chooseDateView.height+10, SCREEN_WIDTH, SCREEN_HEIGHT) titles:titleArr];
	segment.view.backgroundColor = kWhiteColor;
	[segment setViewControllers:vcArr];
	segment.style = CCZSegementStyleDefault;
	[segment setSegementTintColor:UIMainColorRGB];
	[self.contentView addSubview:segment.view];
	[self addChildViewController:segment];
	[segment selectedNavAtIndex:^(NSInteger index) {
		NSLog(@"index %ld",index);
	}];
	
	//选择时间按钮
	self.startDate.text = [NSDate br_getDateString:[NSDate date] format:@"yyyy-MM-dd"];
	self.endDate.text = [NSDate br_getDateString:[NSDate date] format:@"yyyy-MM-dd"];
	[self.startDate addActionWithblock:^{
		NSDate *maxDate =  [NSDate br_getDate:self.endDate.text format:@"yyyy-MM-dd"];
		NSDate *minDate = [NSDate br_setYear:2018 month:1 day:1];
		[BRDatePickerView showDatePickerWithTitle:@"请选择起始时间" dateType:BRDatePickerModeDate defaultSelValue:self.startDate.text minDate:minDate maxDate:maxDate isAutoSelect:NO themeColor:UIMainColorRGB resultBlock:^(NSString *selectValue) {
			self.startDate.text = selectValue;
			if (segment.viewControllers.count == 1) {
				depositRecord.page = 1;
				depositRecord.startTime = selectValue;
			}
			else {
				depositRecord.page = 1;
				incomeDetail.page = 1;
				depositRecord.startTime = selectValue;
				incomeDetail.startTime = selectValue;
			}
		}];
	}];
	[self.endDate addActionWithblock:^{
		NSDate *maxDate = [NSDate date];
		NSDate *minDate = [NSDate br_getDate:self.startDate.text format:@"yyyy-MM-dd"];
		[BRDatePickerView showDatePickerWithTitle:@"请选择结束时间" dateType:BRDatePickerModeDate defaultSelValue:self.endDate.text minDate:minDate maxDate:maxDate isAutoSelect:NO themeColor:UIMainColorRGB resultBlock:^(NSString *selectValue) {
			self.endDate.text = selectValue;
			if (segment.viewControllers.count == 1) {
				depositRecord.page = 1;
				depositRecord.endTime = selectValue;
			}
			else {
				depositRecord.page = 1;
				incomeDetail.page = 1;
				depositRecord.endTime = selectValue;
				incomeDetail.endTime = selectValue;
			}
		}];
	}];
}

#pragma mark ==========响应方法==========
- (void)recordMoney:(id)sender {
	NSLog(@"点击充值记录");
	PerWalletRecordViewController *recordVC = [[PerWalletRecordViewController alloc] init];
	recordVC.type = 4;
	[self.navigationController pushViewController:recordVC animated:YES];
}

/**
 更新用户信息
 */
- (void)updateUserInfo {
	
	[[NetworkTool sharedTool] requestWithName:@"获取用户信息" URLString:APIUser parameters:nil method:@"GET" callBack:^(id responseObject) {
		NSDictionary *userDict = [responseObject valueForKey:@"data"];
		
		self.virtualCoin.text = [NSString stringWithFormat:@"%@", [userDict valueForKey:@"coin"]];
		self.balance.text = userDict[@"withdrawable_money"];
		self.deposit.text = userDict[@"bond"];
		self.withdrawableMoney = userDict[@"withdrawable_money"];
		self.frozenBalance.text = [NSString stringWithFormat:@"冻结金额:%@元",userDict[@"freezing_money"]];
		self.receivedVirtualCoin.text = userDict[@"star"];
	}];
}


- (IBAction)virtualCoinChargeClick:(id)sender {
	PerWalletViewController *virtualCoinChargeVC = [[PerWalletViewController alloc] init];
	[self.navigationController pushViewController:virtualCoinChargeVC animated:YES];
}
- (IBAction)depositChargeClick:(id)sender {
	NSInteger roleNum = [GetUserDefaule(USERINFO)[@"role"] integerValue];
	if (roleNum == 1) {
		[self presentViewController:[LZCTools alertMessage:@"您尚未认证，请认证后再操作"] animated:YES completion:nil];
		return;
	}
	DepositChargeViewController *vc = [[DepositChargeViewController alloc] init];
	[self.navigationController pushViewController:vc animated:YES];
}

- (void)withdrawalClick:(UIButton *)sender {
	NSLog(@"点击提现");
	PerWalletRecordViewController *recordVC = [[PerWalletRecordViewController alloc] init];
	recordVC.type = 2;
	recordVC.largestAmount = [NSString stringWithFormat:@"%.2lf", self.withdrawableMoney.floatValue];
	[self.navigationController pushViewController:recordVC animated:YES];
}
- (IBAction)presentRecordBtnClick:(id)sender {
	NSString *urlStr = [NSString stringWithFormat:@"%@/mobile/gift/index?token=%@", APIBackEndUrl, [GetUserDefaule(Token) valueForKey:@"access_token"]];
	//NSString *urlStr = @"http://www.baidu.com";
	AXWebViewController *vc = [[AXWebViewController alloc] initWithAddress:urlStr];
	[self.navigationController pushViewController:vc animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
